<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <search-bar-new :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :show-summary="true"
          :summary-method="getSummaries"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />
          <el-table-column label="操作" align="center" fixed="right" width="180">
            <template slot-scope="scope">
              <el-button
                type="text"
                @click.stop="detail(scope.row)"
              >查看</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { list } from '@/api/equipmentManage/equipmentStatement/sparePartsRanking'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '备件消耗排行',
      pageExportId: '备件消耗排行导出',
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: '',
        isAsc: 'desc'

      },
      total: 1, // 数据一共有多少条
      mainId: 'id',
      tableHeight: '0',
      tableData: [],
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'rankId', label: '名次', canOrder: false },
        { prop: 'code', label: '备件编码' },
        { prop: 'name', label: '备件名称', canOrder: false },
        { prop: 'model', label: '规格型号', canOrder: false },
        { prop: 'numberUnitLabel', label: '备件单位', canOrder: false },
        { prop: 'expandNumber', label: '消耗数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false }
      ],
      exportTableColumns: [// 表格常规字段（初始值）
        { prop: 'rankId', label: '名次', canOrder: false },
        { prop: 'code', label: '备件编码' },
        { prop: 'name', label: '备件名称', canOrder: false },
        { prop: 'model', label: '规格型号', canOrder: false },
        { prop: 'numberUnitLabel', label: '备件单位', canOrder: false },
        { prop: 'expandNumber', label: '消耗数量', minWidth: this.SETTINGS.settings_decimalDigits_length, canOrder: false }
      ],
      defaultExport: [],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'searchValue', label: '模糊搜索', type: 'input' },
        // { prop: 'code', label: '备件编码', type: 'input' },
        // { prop: 'name', label: '备件名称', type: 'input' },
        // { prop: 'model', label: '规格型号', type: 'input' },
        { prop: 'searchTime', label: '消耗时间', type: 'time', isTime: true, hasDefault: true, defaultKey: 'defaultSearchTime' }
      ],

      operationData: [ // 右上角的操作按钮
        { label: '批量导出3', perm: '' },
        { label: '页面配置', perm: '' }
      ],
      statusOptions: [],
      statusOptionsWarehouse: [],
      defaultSearchTime: [],
      startTime: '',
      endTime: ''
    }
  },
  computed: {
    // 分页器
    selectedNum() {
      return this.$refs.table ? this.$refs.table.selection.length : 0
    }
  },
  created() {
    this.defaultSearchTime = [this.$moment(new Date()).subtract(1, 'months').format('YYYY-MM-DD') + ' 00:00',
      this.$moment(new Date()).format('YYYY-MM-DD') + ' 23:59']
  },
  methods: {
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data.dataTable.data
        this.total = res.data.dataTable.total
        this.loading = false
        this.startTime = res.data.startTime
        this.endTime = res.data.endTime
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    getExportData(data) {
      return new Promise(function(resolve, reject) {
        list(data).then(res => {
          resolve(res.data.dataTable)
        })
      })
    },
    detail(row) {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            id: row.id,
            startTime: this.startTime,
            endTime: this.endTime,
            expandNumber: row.expandNumber
          }// props
        },
        area: ['1080px', '700px'],
        title: '消耗详情', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    getSummaries(param) {
      const {
        columns,
        data
      } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index == 0) {
          sums[index] = '合计'
          return
        }
        if (column.property == 'expandNumber') {
          const values = data.map(item => item.expandNumber)
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
        } else {
          sums[index] = ''
        }
      })
      return sums
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-table__fixed-footer-wrapper tr :last-child{
  top: 70px;
  background-color: transparent!important;
}
/deep/ .el-table__footer-wrapper tbody td.el-table__cell{
  background-color: transparent!important;
}
</style>
